package com.shiguo365.market.display.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.display.adapter.UserCartAdapter;
import com.shiguo365.market.display.base.StatusBarActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.view.ICartView;
import com.shiguo365.market.presenter.contract.ICartPresenter;
import com.shiguo365.market.presenter.implement.CartPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CartActivity extends StatusBarActivity implements ICartView,SwipeRefreshLayout.OnRefreshListener{

    private static final String TAG = "CartActivity";

    @BindView(R.id.cart_toolbar)
    protected Toolbar toolbar;

    @BindView(R.id.cart_recycler_view)
    protected RecyclerView recyclerView;

    @BindView(R.id.cart_refresh_layout)
    protected SwipeRefreshLayout refreshLayout;

    @BindView(R.id.cart_icon_no_data)
    protected TextView iconNoData;

    private List<JSONObject> mObjects = new ArrayList<JSONObject>();

    private UserCartAdapter mAdapter;

    private ICartPresenter mPresenter;

    private Integer uid = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        ButterKnife.bind(this);


        mPresenter = new CartPresenter(this,this);


        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new UserCartAdapter(this, mPresenter,mObjects);
        recyclerView.setAdapter(mAdapter);

        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);
    }


    @Override
    public void onRefresh() {
        Log.e(TAG,"onRefresh()");
        mPresenter.getCartById(uid);
    }


    @Override
    public boolean onGetCartOk(@NonNull JSONObject result) {
//        mObjects.clear();
//        mObjects.addAll(result);
//        mAdapter.notifyDataSetChanged();
//        iconNoData.setVisibility(mObjects.size() == 0 ? View.VISIBLE : View.GONE);
        return true;
    }

    @Override
    public void onGetCartFinished() {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onGetCartFailed() {

    }

    @Override
    public void onDelCartOk() {
        RefreshUtils.refreshOnCreate(refreshLayout, this);
    }
}
